﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 计算器
{
    public partial class Form1 : Form
    {
        private List<int> value_list = new List<int>();//存用户输入的数字
        private List<int> operator_list = new List<int>();//存用户输入的运算符，1234 */+-
        private bool result;
        private bool resign;
        private string sign;

        private int preSign = 0;  //前一个sign位置
        private int nowSign = 0;      //当前sign位置
        private int all = 0;      //当前总字符数
        public Form1()
        {
            InitializeComponent();
            result = false;
            resign = false;
        }

        private void button16_Click(object sender, EventArgs e)
        {
            result = false;
            resign = false;
            textBox1.Text = "";
            preSign = 0;
            nowSign = 0;
            all = 0;
            sign = "@";
            value_list.Clear();
            operator_list.Clear();
        }

        private void numClick(object sender, EventArgs e)
        {
            if (!result)
            {
                Button btn = (Button)sender;
                textBox1.Text = textBox1.Text + btn.Text;
                all++;
                resign = false;
            }
            else
            {
                MessageBox.Show("请清空");
            }
        }

        private void signClick(object sender, EventArgs e)
        {
            if (!resign)
            {
                if (!result)
                {
                    all++;
                    preSign = nowSign;
                    nowSign = all;
                    Button btn = (Button)sender;
                    textBox1.Text = textBox1.Text + btn.Text;
                    sign = btn.Text;
                    string s = btn.Text;
                    switch (s)
                    {
                        case "*":
                            operator_list.Add(1);
                            break;
                        case "/":
                            operator_list.Add(2);
                            break;
                        case "+":
                            operator_list.Add(3);
                            break;
                        case "-":
                            operator_list.Add(4);
                            break;
                        default:
                            MessageBox.Show("出错了");
                            return;
                    }
                    resign = true;
                    

                    if (preSign +1== nowSign) {
                        if (s == "*" || s == "/")
                        {
                            MessageBox.Show("输入错误");

                        }
                        else
                        {
                            value_list.Add(0);
                        }
                    }
                    else
                    {
                        value_list.Add(int.Parse(textBox1.Text.Substring(preSign, nowSign - preSign-1)));  //解决多位数数字问题
                    }
                }
                else
                {
                    MessageBox.Show("请清空");
                }
            }
        }

        private void equalClick(object sender, EventArgs e)
        {
            if (!resign)
            {
                if (!result)
                {
                    if (value_list.Count > 0 && operator_list.Count > 0&&nowSign!=all)
                    {//需要防止用户没输入数字，或者只输入了一个数，就按=。
                        preSign = nowSign;
                        nowSign = all;
                        value_list.Add(int.Parse(textBox1.Text.Substring(preSign, nowSign - preSign)));
                        textBox1.Text = textBox1.Text + "=";
                        int total = value_list[0];
                        for (int i = 0; i < operator_list.Count; i++)
                        {
                            int _operator = operator_list[i];//operator是C#的运算符重载的关键字，前面加个_来区别
                            switch (_operator)
                            {
                                case 1:
                                    total *= value_list[i + 1];
                                    break;
                                case 2:
                                    total /= value_list[i + 1];
                                    break;
                                case 3:
                                    total += value_list[i + 1];
                                    break;
                                case 4:
                                    total -= value_list[i + 1];
                                    break;
                            }
                        }
                        textBox1.Text = textBox1.Text + total.ToString();
                        result = true;
                    }
                }
                else
                {
                    MessageBox.Show("请清空");
                }
            }
        }
    }
}
